﻿using IWMAG.Scripting.AST.ObjectModel;
using IWMAG.Scripting.Helpers;
using IWMAG.Scripting.Compiler;
using IWMAG.Scripting.Types;
using System.Collections.Generic;
using IWMAG.Scripting.AST.Declarations;
using System;
using IWMAG.Scripting.AST.Statements;
using System.Reflection;

namespace IWMAG.Scripting.AST {
    public class ExternDeclaration : CodeDeclaration {
        public Signature Signature;
        protected object Instance;
        protected MethodBase Method;

        public ExternDeclaration(Compiler.Lexeme l, object inst, MethodBase method)
            : base(l) {

            if (method == null) {
                throw new MethodAccessException();
            }

            Instance = inst;
            Method = method;
            Signature = Marshaller.GetSignature(Method, new CompileTable());
        }

        public override void Verify(Helpers.CompileTable sym, List<Compiler.IUserInfo> error) {
        }

        public object Invoke(params object[] args) {
            if (Method is MethodInfo) {
                return Method.Invoke(Instance, args);
            } else {
                return Activator.CreateInstance(Method.DeclaringType, args);
            }
        }
    }
}